<?php

namespace app\index\controller;

use api\message\Rest;
use Predis\Client;
use think\Model;
use think\Request;

class Register
{
    // 手机号验证
    public function tel()
    {
        $tel = Request::instance()->post();
        $model = Model('register');
        $res = $model->tel($tel);

        if($res != null){
            $data = [
              'status' => false,
              'msg' => '手机号码已经被注册',
            ];
            return json($data);
        } else {
            $data = [
                'status' => true,
            ];
            return json($data);
        }
    }

    // 短信验证
    public function yzm(Request $request)
    {
        $tel = $request->post()['tel'];
        $ip = Request::instance()->ip();
        $strIp = ip2long($ip);
        $num = mt_rand('100000','999999');
        $res = $this->test($tel, array($num, '10'), '1');

        if($res['status']){
        	$redis = new Client();
            $redis->setex('yzm:'.$strIp, 600, $num);
        }
        return json($res);
    }

    // 注册信息
    public function register()
    {
        $data =  Request::instance()->post();

        $ip = Request::instance()->ip();
        $strIp = ip2long($ip);
		    
		$redis = new Client();
		$yzm = $redis->get('yzm:'.$strIp);
		if(empty($yzm) || ($yzm != $data['verification'])){
		   $data = [
		       'status' => false,
		       'msg' => '验证码不正确',
		   ];
		   return json($data);
		}

		// 删除redis中的验证码
		$redis->del('yzm:'.$strIp);
		
        $info = [
            'tel' => $data['tel'],
            'nickname' => $data['nickname'],
            'pass' => MD5($data['pass']),
            'sex' => $data['sex'],
            'province' => $data['province'],
            'city' => $data['city'],
            'area' => $data['area'],
            'last_login_time' => time(),
            'register_time' => time(),
            'times' => 1,
            'login_ip' => $ip,
        ];
        $model = Model('register');
        $res = $model->index($info);

        if($res){
            $data = [
                'status' => true,
                'msg' => '注册成功',
            ];

            // 设置自增id
            $id = $redis->incr('index:id');
            // 将用户数据存进session中
            session('index', ['pwd'=>$info['pass'],'tel'=>$info['tel']]);
            // 将用户ID存入redis
            $redis->setex('index:name:' . $info['tel'] . ':id', '7200', $id);
            // 将用户信息以自增 id 存入redis(hash)
            $redis->hset('index:' . $id, 'pwd', $info['pass']);
            $redis->hset('index:' . $id, 'name', $info['nickname']);
            $redis->hset('index:' . $id, 'id', $res);
            $redis->hset('index:' . $id, 'times', $info['times']);

            return $data;
        } else {
            $data = [
                'status' => false,
                'msg' => '注册失败,请稍后再试',
            ];
            return json($data);
        }
    }

    // 容量云验证
    public function test($to, $datas, $tempId)
    {
        $accountSid = '8a216da86010e69001601f6e21b90387';
        $accountToken = 'd3180e1ac32642cf96efe67ddb4cfdc8';
        $appId = '8a216da86010e69001601f6e2216038e';
        $serverIP = 'app.cloopen.com';
        $serverPort = '8883';
        $softVersion = '2013-12-26';

        $rest = new Rest($serverIP, $serverPort, $softVersion);
        $rest->setAccount($accountSid, $accountToken);
        $rest->setAppId($appId);

        // 发送模板短信
        $result = $rest->sendTemplateSMS($to, $datas, $tempId);
        if ($result == NULL) {
            return "result error!";
        }
        if ($result->statusCode != 0) {
            $data = [
					'status'=>false,
					"error code :" . $result->statusCode . "<br>",
					"error msg :" . $result->statusMsg . "<br>",
                ];
            //TODO 添加错误处理逻辑
            return $data;
        } else {
            $data = [
                'status'=>true,
                "Sendind TemplateSMS success!<br/>",
	            // 获取返回信息
	            $smsmessage = $result->TemplateSMS,
	             "dateCreated:" . $smsmessage->dateCreated . "<br/>",
	            "smsMessageSid:" . $smsmessage->smsMessageSid . "<br/>",
            ];
            //TODO 添加成功处理逻辑
            return $data;
        }
    }
}
